package cn.xuyun.cc.tcp.coder;

import org.apache.log4j.Logger;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelDownstreamHandler;

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;

import cn.xuyun.cc.tcp.util.LogUtil;
import cn.xuyun.cc.tcp.vo.Entity;

public class MyEncoder extends SimpleChannelDownstreamHandler{

	
	private static final Logger logger=Logger.getLogger(MyEncoder.class);
	@Override
	public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

		
		Entity entity= (Entity) e.getMessage();
		logger.info("send to client");
		LogUtil.log(logger, entity);
		
		ChannelBuffer totalBuffer=ChannelBuffers.dynamicBuffer();
		
		totalBuffer.writeLong(entity.getMagic());//魔术字
		totalBuffer.writeInt(entity.getVersion());
		totalBuffer.writeInt(entity.getData_len());
		totalBuffer.writeInt(entity.getSend_time());
		totalBuffer.writeInt(entity.getSerial());
		totalBuffer.writeInt(entity.getType());
		totalBuffer.writeInt(entity.getCmd());
		totalBuffer.writeInt(entity.getContent_len());
		totalBuffer.writeBytes(entity.getContent());
		
		totalBuffer.writeInt(entity.getExtra());
		
		Channels.write(ctx, e.getFuture(), totalBuffer);

	}
}
